package com.cqupt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.mapper.ArticleRecordMapper;
import com.cqupt.mapper.ScoreRecordMapper;
import com.cqupt.mapper.UserArticleMapper;
import com.cqupt.model.pojo.Article;
import com.cqupt.model.pojo.ArticleRecord;
import com.cqupt.model.pojo.ScoreRecord;
import com.cqupt.model.pojo.UserArticle;
import com.cqupt.model.vo.ArticleVO;
import com.cqupt.model.vo.PageVo;
import com.cqupt.model.vo.ScoreVO;
import com.cqupt.service.ArticleService;
import com.cqupt.mapper.ArticleMapper;
import com.cqupt.service.SparkService;
import com.cqupt.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
        implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private UserArticleMapper userArticleMapper;

    @Resource
    private ArticleRecordMapper articleRecordMapper;


    @Resource
    private SparkService sparkService;


    @Resource
    private ScoreRecordMapper scoreRecordMapper;

    @Override
    public PageVo<Article> category(int categoryId, Integer pageNum, Integer pageSize) {
        PageVo<Article> pageVo = new PageVo<>();
        if (categoryId == 1) {
            List<Article> articles = articleMapper.selectList(null);
            pageVo.setRecords(articles);
            return pageVo;
        }
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getCategoryId, categoryId);
        wrapper.orderByDesc(Article::getTime);
        if (pageNum != null && pageSize != null) {
            Page<Article> page = new Page<>(pageNum, pageSize);
            List<Article> records = articleMapper.selectPage(page, wrapper).getRecords();
            pageVo.setRecords(records);
            pageVo.setTotal(page.getPages());
            return pageVo;
        }
        List<Article> articles = articleMapper.selectList(wrapper);
        pageVo.setRecords(articles);
        return pageVo;
    }

    @Override
    @Transactional
    public ArticleVO getArticle(int id) {
        Integer userId = AuthContextUtil.get().getId();
        Article article = articleMapper.selectById(id);
        LambdaQueryWrapper<UserArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserArticle::getUserId, userId);
        wrapper.eq(UserArticle::getArticleId, id);
        UserArticle userArticle = userArticleMapper.selectOne(wrapper);
        ArticleVO articleVO = new ArticleVO();
        BeanUtil.copyProperties(article, articleVO);
        if (ObjectUtil.isNotNull(userArticle)) {
            articleVO.setIs_mark(1);
        }
        new Thread(() -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String formatDate = sdf.format(date);
            LambdaQueryWrapper<ArticleRecord> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(ArticleRecord::getUserId, userId);
            wrapper1.eq(ArticleRecord::getArticleId, id);
            wrapper1.eq(ArticleRecord::getDate, formatDate);
            ArticleRecord articleRecord = articleRecordMapper.selectOne(wrapper1);
            if (ObjectUtil.isNull(articleRecord)) {
                ArticleRecord articleRecord1 = new ArticleRecord();
                articleRecord1.setUserId(userId);
                articleRecord1.setArticleId(id);
                articleRecord1.setDate(formatDate);
                articleRecordMapper.insert(articleRecord1);
            }
        }).start();


        return articleVO;
    }

    @Override
    public ScoreVO follow(String text, MultipartFile file) {

        ScoreVO scoreVO = sparkService.evaluate(file, text);

        Integer userId = AuthContextUtil.get().getId();

        // 将得分情况保存
        ScoreRecord scoreRecord = new ScoreRecord();
        Integer totalScore = Integer.parseInt(scoreVO.getTotalScore().split("\\.")[0]);
        Integer accuracyScore = Integer.parseInt(scoreVO.getAccuracyScore().split("\\.")[0]);
        Integer fluencyScore = Integer.parseInt(scoreVO.getFluencyScore().split("\\.")[0]);
        Integer integrityScore = Integer.parseInt(scoreVO.getIntegrityScore().split("\\.")[0]);
        scoreRecord.setTotalScore(totalScore);
        scoreRecord.setAccuracyScore(accuracyScore);
        scoreRecord.setFluencyScore(fluencyScore);
        scoreRecord.setIntegrityScore(integrityScore);
        scoreRecord.setUserId(userId);
        scoreRecordMapper.insert(scoreRecord);
        return scoreVO;
    }
}




